<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<?php
// digiboard by digitalboom.org
// Information Technology program
// Sirindhorn International Institute of Technology
// http://www.digitalboom.org
//
// first created on: 2001 Dec xx
// last modified on: 2002 May 14

include("settings/accounts.inc.php");
include("lib/util.inc.php");

echo "x-ansnum-sync v.1.0 by bact'<br>";
echo "for fixing the unsync topic answer numbers<br><br>";
echo "start at ".date("r")."<br>";
echo "synchronizing the answer numbers<br>";
echo "please wait...<br>";

dgb_db_connect();

$topics_query = "SELECT Topic_ID FROM ".$DGB["DB_TOPICS_TABLE"];
$topics_result = mysql_query($topics_query);

while($topics = mysql_fetch_array($topics_result)) {
 $Topic_ID = $topics[0];
 echo "<br>Topic_ID = $Topic_ID<br>";
 $c_query = "SELECT COUNT(*), MAX(PostTime) FROM ".$DGB['DB_ANSWERS_TABLE'].
   " WHERE Topic_ID='$Topic_ID'";

 $g_query = "SELECT AnsNum, AnsTime FROM ".$DGB['DB_TOPICS_TABLE'].
  " WHERE Topic_ID='$Topic_ID'";

 // this loop is for make sure that the answer number
 // is really sync with the actual figure
 do {
  $c_ansnum = 0;
  $c_anstime = "0000-00-00 00:00:00";
  $g_ansnum = 0;
  $g_anstime = "0000-00-00 00:00:00";

  // count
  $c_result  = mysql_query($c_query);
  $c_row     = mysql_fetch_row($c_result);
  $c_ansnum  = $c_row[0];
  $c_anstime = $c_row[1];
  if ($c_anstime == "")
   $c_anstime = "0000-00-00 00:00:00";
   
  echo "$c_query<br>";
  echo "c_ansnum, c_anstime = $c_ansnum, $c_anstime<br>";

  // update
  // ($query should be in the do loop,
  //  else the $c_ansnum variable in the $query will not be updated)
  $query =
   "UPDATE ".$DGB['DB_TOPICS_TABLE'].
   " SET AnsNum='".$c_ansnum."', AnsTime='".$c_anstime."' WHERE Topic_ID='".$Topic_ID."'";
  $result = mysql_query($query);
  echo "$query<br>";

  // verify
  $g_result  = mysql_query($g_query);
  $g_row     = mysql_fetch_row($g_result);
  $g_ansnum  = $g_row[0];
  $g_anstime = $g_row[1];
  if ($g_anstime == "")
   $g_anstime = "0000-00-00 00:00:00";

  echo "$g_query<br>";
  echo "g_ansnum, g_anstime = $g_ansnum, $g_anstime<br>";

  //mysql_free_result($result);
  //mysql_free_result($c_result);
  //mysql_free_result($g_result);
 } while (($c_ansnum != $g_ansnum) || ($c_anstime != $g_anstime));
}
echo "<br><br><b>Finished</b><br>";
echo "stop at ".date("r")."<br><br>";
echo "good luck!";
?>